package cn.iocoder.yudao.module.system.config.transcoder;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

@Configuration
@EnableAsync
public class TranscoderTask {

    /**
     * 音频剪辑任务
     */
    @Bean(name = "cutterTaskExecutor")
    public ThreadPoolTaskExecutor cutterExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //线程池大小
        executor.setCorePoolSize(5);
        //最大线程池大小
        executor.setMaxPoolSize(10);
        //任务队列容量
        executor.setQueueCapacity(25);
        //线程前缀名
        executor.setThreadNamePrefix("cutterTaskExecutor-");
        // 任务拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }

    /**
     * 智能提取任务
     */
    @Bean(name = "extractTaskExecutor")
    public ThreadPoolTaskExecutor extractExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //线程池大小
        executor.setCorePoolSize(5);
        //最大线程池大小
        executor.setMaxPoolSize(10);
        //任务队列容量
        executor.setQueueCapacity(25);
        //线程前缀名
        executor.setThreadNamePrefix("extractTaskExecutor-");
        // 任务拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }

    /**
     * 视频转码任务
     */
    @Bean(name = "transcoderTaskExecutor")
    public ThreadPoolTaskExecutor taskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        //线程池大小
        executor.setCorePoolSize(5);
        //最大线程池大小
        executor.setMaxPoolSize(10);
        //任务队列容量
        executor.setQueueCapacity(25);
        //线程前缀名
        executor.setThreadNamePrefix("transcoderTaskExecutor-");
        // 任务拒绝策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(60);
        executor.initialize();
        return executor;
    }
}
